package com.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.demo.common.model.SecurityUser;
import com.demo.entity.Permission;
import com.demo.entity.User;
import com.demo.mapper.PermissionMapper;
import com.demo.mapper.RoleMapper;
import com.demo.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.Set;

@Service
@Slf4j
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    private RoleMapper roleMapper;
    /**
     * 设置security安全框架走自己的认证流程
     * @param username
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userMapper.selectOne(new QueryWrapper<User>()
                .eq("login_act", username));
        // 判断用户是否存在
        if (ObjectUtils.isEmpty(user)){
            throw new UsernameNotFoundException("用户名或密码有误");
        }

        Set<String> permissionListUser =  permissionMapper.selectPermissionListByUserId(user.getId());
        // 获取登录用户的角色列表
        Set<String> roles = roleMapper.selectRoleListByUserId(user.getId());
        // 获取登录用户的菜单权限
        Set<Permission> menuPermissionList = permissionMapper.selectMenuPermissionListByUserId(user.getId());

        Collection<? extends GrantedAuthority> authorities = Collections.emptyList();
        // 创建 security框架安全用户对象
        SecurityUser securityUser = SecurityUser.builder()
                .id(user.getId())
                .loginAct(username)
                .loginPwd(user.getLoginPwd())
                .name(user.getName())
                .phone(user.getPhone())
                .email(user.getEmail())
                .accountNoLocked(user.getAccountNoLocked())
                .accountNoExpired(user.getAccountNoExpired())
                .credentialsNoExpired(user.getCredentialsNoExpired())
                .accountEnabled(user.getAccountEnabled())
                .createTime(user.getCreateTime())
                .createBy(user.getCreateBy())
                .editTime(user.getEditTime())
                .editBy(user.getEditBy())
                .lastLoginTime(user.getLastLoginTime())
                .authorities(authorities)
                .permissionList(permissionListUser)
                .roles(roles)
                .menuPerms(menuPermissionList)
                .build();
        log.info("用户信息{}",securityUser.toString());
        return securityUser;
    }
}
